/*************************************************************************
 *
 * $Id: 1wire.h 55 2006-07-22 09:43:13Z alpov $
 * 
 *************************************************************************/         

#ifndef _1WIRE_H
#define _1WIRE_H

/* LOW LEVEL */
extern unsigned char OWReset(void);
extern void OWWriteBit(unsigned char bit);
extern unsigned char OWReadBit(void);
extern void OWStrongPullup(unsigned char on_off);
extern void OWWriteByte(unsigned char data);
extern unsigned char OWReadByte(void);
extern void OWSendCmd(unsigned char *ROM, unsigned char cmd);
extern char OWFirst(unsigned char *ROM);
extern char OWNext(unsigned char *ROM);
extern void OWCRC(unsigned char x, unsigned char *crc);

/* HIGH LEVEL */
extern void OWConvertAll(unsigned char wait);
extern unsigned char OWReadTemperature(unsigned char *ROM, int *temperature);

/* BUS CONNECTION */
#define OWIREOUTPORT    PORTD
#define OWIREINPORT     PIND
#define OWIREDDR        DDRD
#define OWIREOUTPIN     PD6
#define OWIREINPIN      PD6

/* INTERNAL CONSTANTS everything below this line */

/* Return codes for OWFirst()/OWNext() */
#define OW_BADWIRE      -3
#define OW_BADCRC       -2
#define OW_NOPRESENCE   -1
#define OW_NOMODULES    0
#define OW_FOUND        1

/* General 1 wire commands */
#define OW_SEARCH_ROM_CMD   0xF0
#define OW_READ_ROM_CMD     0x33
#define OW_MATCH_ROM_CMD    0x55
#define OW_SKIP_ROM_CMD     0xCC

/* DS1820 commands */
#define OW_CONVERT_T_CMD    0x44
#define OW_RD_SCR_CMD       0xBE
#define OW_WR_SCR_CMD       0x4E

/* Family codes */
#define OW_FAMILY_TEMP      0x10
// fixme

/* 1-wire delays, check F_CPU is set correctly */
#define DELAY_A _delay_us(6)
#define DELAY_B _delay_us(64)
#define DELAY_C _delay_us(60)
#define DELAY_D _delay_us(10)
#define DELAY_E _delay_us(9)
#define DELAY_F _delay_us(55)
#define DELAY_G
#define DELAY_H _delay_ms(0.480)
#define DELAY_I _delay_us(70) 
#define DELAY_J _delay_ms(0.410)

/* Other */
#define CONVERT_T_DELAY 750

#endif
